<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>个人信息</title>
	<meta name="renderer" content="webkit">	
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">	
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">	
	<meta name="apple-mobile-web-app-status-bar-style" content="black">	
	<meta name="apple-mobile-web-app-capable" content="yes">	
	<meta name="format-detection" content="telephone=no">	
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/common/layui/css/layui.css" media="all">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/common/bootstrap/css/bootstrap.css" media="all">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/common/global.css" media="all">
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style/css/personal.css" media="all">
</head>
<body>
<section class="layui-larry-box">
	<div class="larry-personal">
	    <div class="layui-tab">
            <blockquote class="layui-elem-quote news_search">

        <%--模糊查询 --%>
        <form action="#">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input id="keyWord" name="titlekey" value="${empty requestScope.titlekey?'':requestScope.titlekey}" placeholder="请输入姓名关键字" class="layui-input search_input" type="text">
                </div>
                <input type="button" id="searchBtn" name="searchBtn" value="查询" class="layui-btn search_btn"/>
                <div class="layui-input-inline">
                    <input  type="button" id="clearBtn" class="layui-btn search_btn" value="清除"/>
                </div>
            </div>
        </form>
	</blockquote>
            
		         <!-- 操作日志 -->
                <div class="layui-form news_list">
                    <table class="layui-table">
					    <colgroup>
						<col width="9%">
						<col width="9%">
						<col width="9%">
						<col width="9%">
						<col width="9%">
						<col width="15%">
					</colgroup>
					<thead>
						<tr>
							<th>ID</th>
							<th>管理员名</th>
							<th>密码</th>
							<th>等级</th>
							<th>状态</th>
							<th>操作</th>
						</tr>
					</thead>
                        <tbody class="news_content" id="momentsListBody">
<%--    						<tr>
                                <td>admin.adminId</td>
                                <td>admin.adminName</td>
                                <td>admin.adminPwd</td>
                                <td>admin.adminLevel</td>
                                <td>admin.adminStatus</td>
                                <td>
                                    <a href="" class="layui-btn layui-btn-mini news_edit" data-toggle="modal" data-target="#myModal"
                                        data-adminId="${admin.adminId }"
                                        data-adminName="${admin.adminName }"
                                        data-adminPwd="${admin.adminPwd }"
                                        data-adminLevel="${admin.adminLevel }"
                                        data-adminStatus="${admin.adminStatus }" onclick="showUpdateDate(this)">
                                        <i class="iconfont icon-edit"></i> 编辑
                                    </a>
                                    <a href="javascript:delFun(admin.adminId)" class="layui-btn layui-btn-danger layui-btn-mini news_del" data-id="1">
                                        <i class="layui-icon"></i> 删除
                                    </a>
                                </td>
                            </tr>--%>
                        </tbody>
					</table>
                    <!-- 分页信息 -->
                    <div id="showPageInfo"></div>
                    <!-- 分页实现    -->
                    <div class="col-md-12 text-center"><!-- 设置居中 -->
                        <ul class="pagination" id="pageItem">

                        </ul>
                    </div>
                </div>
        </div>
	</div>
</section>
<!-- 放最后面 -->
<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <form class="form-horizontal" role="form" >
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">修改管理员信息</h4>
                </div>
                <div class="modal-body">

                    <div class="form-group">
                        <label for="adminId" class="col-sm-2 control-label">管理员编号</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" id="adminId"
                                   name="adminId" readonly="readonly" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="adminName" class="col-sm-2 control-label">管理员名</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" id="adminName"
                                   name="adminName" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="adminPwd" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-8">
								<textarea rows="5" class="form-control" name="adminPwd"
                                          id="adminPwd"></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="adminLevel" class="col-sm-2 control-label">等级</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" id="adminLevel"
                                   name="adminLevel" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="adminStatus" class="col-sm-2 control-label">状态</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" id="adminStatus"
                                   name="adminStatus" />
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="button" class="btn btn-primary" id="updatebtn">提交更改</button>
                </div>
            </div>
            <!-- /.modal-content -->
        </form>
    </div>
</div>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/style/common/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/style/js/newslist.js"></script>


    <script type="text/javascript">
            //声明一个全局变量key
            var key="";

            $(function(){
                $.ajaxSetup({
                    complete: function (xhr, status) {
                        //拦截器实现超时跳转到登录页面
                        // 通过xhr取得响应头
                        var REDIRECT = xhr.getResponseHeader("REDIRECT");
                        //如果响应头中包含 REDIRECT 则说明是拦截器返回的
                        if (REDIRECT == "REDIRECT") {
                            var win = window;
                            while (win != win.top) {
                                win = win.top;
                            }
                            //重新跳转到 login.html
                            win.location.href = xhr.getResponseHeader("CONTENTPATH");
                        }
                    }
                });
                //赋值动作,各种监听事件
                //界面一进来就需要发送ajax请求文章列表
                $.get("${pageContext.request.contextPath }/AdminController/adminPage",function(result){
                    //显示数据写成一个方法,以后就不用在做拼接
                    showData(result);
                },"json");

				//首页
				$(document).on("click",".first",function(){
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:1,pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json");
				});
				//末页
				$(document).on("click",".last",function(){
					var totalPage=$("#totalPage").text();
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:totalPage,pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json");
				});
				//上一页
				$(document).on("click",".up",function(){
					var currentPage=$("#currentPage").text();
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:(parseInt(currentPage)-1),pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json")
				});
				//下一页
				$(document).on("click",".next",function(){
					var currentPage=$("#currentPage").text();
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:(parseInt(currentPage)+1),pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json");
				});
				//每一页
				$(document).on("click",".currentPage",function(){
					var pageNum=$(this).text();
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:pageNum,pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json")
				});
				//跳转
				$(document).on("click",".trunto",function(){
					var pageNum=$("#turntoId").val();
					var totalPage=$("#totalPage").text();
					if(pageNum<1){
						pageNum=1;
						$("#turntoId").val(pageNum);
					}else if(pageNum>totalPage){
						pageNum=totalPage;
						$("#turntoId").val(pageNum);
					}
					//发送ajax请求
					$.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:pageNum,pageSize:3,titleKey:key},function(result){
						showData(result);
					},"json")
				});

                //搜索按钮的模糊查询
                $("#searchBtn").click(function(){
                    //用户输入的模糊查询关键字
                    key=$("#keyWord").val();
                    //发送ajax请求  首页
                    $.get("${pageContext.request.contextPath }/AdminController/adminPage",{pageNum:1,pageSize:3,titlekey:key},function(result){
                        //显示数据写成一个方法,以后就不用在做拼接
                        showData(result);
                    },"json")
                });
                //清除按钮实现
                $("#clearBtn").click(function(){
                    key="";
                    $("#keyWord").val("");
                })

            });

            function showData(result){
                //1.每一次遍历之前都要清空
                $("#momentsListBody").empty();
                $("#showPageInfo").empty();
                $("#pageItem").empty();
                //2.显示文章列表数据+"
                $.each(result.list,function(index,admin){
                var level="";
                var status="";
                if(admin.adminLevel==2){
                    level="<span class='layui-btn layui-btn-danger layui-btn-mini'>超级管理员</span>";
                }else{
                    level="<span class='layui-btn layui-btn-danger layui-btn-mini'>普通管理员</span>";
                }
                if(admin.adminStatus==1){
                    status="<span class='layui-btn layui-btn-mini news_edit'>启用</span>";
                }else{
                    status="<span class='layui-btn layui-btn-danger layui-btn-mini'>禁用</span>";
                }
				//先把tr标签复制到后端的字符集中，自动转义，复制到txt文本中替换其中的${  与    }
				$("#momentsListBody").append("\t\t\t\t\t\t<tr>\n" +
						"                                <td>"+admin.adminId+"</td>\n" +
						"                                <td>"+admin.adminName+"</td>\n" +
						"                                <td>"+admin.adminPwd+"</td>\n" +
						"                                <td>"+level+"</td>\n" +
						"                                <td>"+status+"</td>\n" +
						"                                <td>\n" +
						"                                    <a class=\"layui-btn layui-btn-mini\" data-toggle=\"modal\" data-target=\"#myModal\"\n" +
						"                                        data-adminid=\""+admin.adminId+"\"\n" +
						"                                        data-adminname=\""+admin.adminName+"\"\n" +
						"                                        data-adminpwd=\""+admin.adminPwd+"\"\n" +
						"                                        data-adminlevel=\""+admin.adminLevel+"\"\n" +
						"                                        data-adminstatus=\""+admin.adminStatus +"\" onclick=\"showUpdateData(this)\">\n" +
						"                                        <button class=\"layui-btn layui-btn-mini\"><i class=\"iconfont icon-edit\"></i>编辑</button>\n" +
						"                                    </a>\n" +
						"                                    <a href=\"javascript:delFun("+admin.adminId+")\" class=\"layui-btn layui-btn-danger layui-btn-mini\" data-id=\"1\">\n" +
						"                                        <i class=\"layui-icon\"></i> 删除\n" +
						"                                    </a>\n" +
						"                                </td>\n" +
						"                            </tr>")
				});
				//显示分页信息
				$("#showPageInfo").append("当前第<span id='currentPage'>"+result.pageNum+"</span>页/共<span id='totalPage'>"+result.pages+
						"</span>页/每页显示<span id='pageSize'>"+result.pageSize+"</span>条/共<span id='totalCount'>"+result.total+"</span>条数据");

				//首页
				if(1==result.pageNum){
					$("#pageItem").append('<li class="disabled"><a href="#">首页</a></li>');
				}else{
					$("#pageItem").append('<li><a href="#" class="first">首页</a></li>');
				}
				//上一页
				if(1==result.pageNum){
					$("#pageItem").append('<li class="disabled"><a href="#">上一页</a></li>');
				}else{
					$("#pageItem").append('<li><a href="#" class="up">上一页</a></li>');
				}
				//显示分页栏目
				for(var i=1;i<=result.pages;i++){
					if(i==result.pageNum){
						$("#pageItem").append('<li class="active"><a href="#">'+i+'</a></li>');
					}else{
						$("#pageItem").append('<li><a href="#" class="currentPage">'+i+'</a></li>');
					}
				}
				//下一页
				if(result.pageNum==result.pages){
					$("#pageItem").append('<li class="disabled"><a href="#">下一页</a></li>');
				}else{
					$("#pageItem").append('<li><a href="#" class="next">下一页</a></li>');
				}
				//末页
				if(result.pageNum==result.pages){
					$("#pageItem").append('<li class="disabled"><a href="#">末页</a></li>');
				}else{
					$("#pageItem").append('<li><a href="#" class="last">末页</a></li>');
				}
				//跳转页
				$("#pageItem").append('<li ><a href="#"><input type="number" id="turntoId" value="'+result.pageNum+'" min="1" max="'+result.pages+
						'" style="width:50px;height:20px; margin-bottom: 0px" /></a></li>');
				$("#pageItem").append('<li><a href="#" class="trunto">跳转</a></li>');
			}

            //在模态框显示要修改的数据
            function showUpdateData(obj) {
                var adminId = $(obj).data("adminid");
                var adminName = $(obj).data("adminname");
                var adminPwd = $(obj).data("adminpwd");
                var adminLevel = $(obj).data("adminlevel");
                var adminStatus = $(obj).data("adminstatus");
                //显示
                $("#adminId").val(adminId);
                $("#adminName").val(adminName);
                $("#adminPwd").val(adminPwd);
                $("#adminLevel").val(adminLevel);
                $("#adminStatus").val(adminStatus);
                //性别不可修改
                //$("#sex").attr('onClick','javascript:return false');
            }

            //模态框提交修改的监听事件
            $("#updatebtn").click(function(){
                //创建对象存放修改后的数据
                /*var admin={
                    adminId:$("#adminId").val(),
                    adminName:$("#adminName").val(),
                    adminPwd:$("#adminPwd").val(),
                    adminLevel:$("#adminLevel").val(),
                    adminStatus:$("#adminStatus").val(),
                };*/
                //发送ajax修改操作
                $.post("${pageContext.request.contextPath}/AdminController/updateAdminInfo",
                    {
                        // admin:JSON.stringify(admin)
                        adminId:$("#adminId").val(),
                        adminName:$("#adminName").val(),
                        adminPwd:$("#adminPwd").val(),
                        adminLevel:$("#adminLevel").val(),
                        adminStatus:$("#adminStatus").val()
                    },
                    function(flag){
                        if(flag){
                            //关闭模态框
                            $("#myModal").modal('hide');
                            //刷新数据,发送ajax请求,显示当前页
                            $.get("${pageContext.request.contextPath}/AdminController/adminPage",
                                {
                                    pageNum:$("#pageNum").text(),
                                    pageSize:3,
                                    titleLike:key
                                },
                                function(result){
                                    //将html拼接写成方法
                                    showData(result);
                                },"json")
                        }else{
                            alert("修改失败")
                        }
                    })

            })

            //删除
            function delFun(adminId) {
                var flag = confirm("是否确定删除文章编号为:" + adminId);
                if (flag) {
                    //发起ajax请求
                    $.get("${pageContext.request.contextPath }/AdminController/delAdminById",{adminId:adminId},function(data){
                        if(data){
                            //刷新数据 发送ajax请求  显示当前改的那一页
                            $.get("${pageContext.request.contextPath }/AdminController/adminPage",
                                {
                                    pageNum:$("#pageNum").text(),//当前页
                                    pageSize:3,
                                    titleKey:key
                                },function(result){
                                    //显示数据写成一个方法,以后就不用在做拼接
                                    showData(result);
                                },"json")
                        }else{
                            alert("删除失败!")
                        }
                    },"json")
                }
            }
    </script>
</body>
</html> 